package blog.controller;

import blog.entity.CateGory;
import blog.service.ArticleService;
import blog.service.CateGoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller @RequestMapping("/category")
public class CateGoryController {
    @Resource
    private CateGoryService cateGoryService;
    @Resource
    private ArticleService articleService;
    @RequestMapping("")
    public String listGory(Map map){
        List<CateGory> cateGoryList = cateGoryService.findAll();
        map.put("cateGoryList",cateGoryList);
        return "category/category-list";
    }
    @RequestMapping("/insertSubmit")
    public String insertCateGory(HttpServletRequest request,Map map){
        String categoryName = request.getParameter("cateGoryName");
        int categoryPid = Integer.parseInt(request.getParameter("categoryPid"));
        String categoryDescription = request.getParameter("categoryDescription");
        String categoryIcon = request.getParameter("categoryIcon");
        CateGory cateGory = new CateGory();
        cateGory.setCateGoryPid(categoryPid);
        cateGory.setCateGoryName(categoryName);
        cateGory.setCateGoryDescription(categoryDescription);
        cateGory.setCateGoryIcon(categoryIcon);

        cateGoryService.insertCateGory(cateGory);
        map.put("cateGory",cateGory);
        map.put("operatorId", String.valueOf(categoryPid));
        map.put("msg","添加成功");
        return "forward:/category";
    }
    @RequestMapping("/delete/{id}")
    public String deleteCateGory(@PathVariable("id") String cateGoryId,Map map){
        cateGoryService.deleteGoryById(cateGoryId);
        map.put("msg","删除成功");
        return "forward:/category";
    }
    @RequestMapping("/validateCateGoryName") @ResponseBody
    public String validateCateGoryName(String cateGoryName){
         CateGory cateGory = cateGoryService.selectGoryByName(cateGoryName);
         return cateGory!=null?"-1":"";
    }
    @RequestMapping("/edit/{id}")
    public String gotoEdit(@PathVariable("id") Integer id,Map map){
        CateGory cateGory = cateGoryService.getCateGoryById(id);
        List<CateGory> cateGoryList = cateGoryService.findAll();
        int articleCount = articleService.getArticleCateGoryCount(id);
        map.put("articleCount",articleCount);
        map.put("cateGoryList",cateGoryList);
        map.put("cateGory",cateGory);
        map.put("operatorId",cateGory.getCateGoryPid());
        return "category/category-edit";
    }
    @RequestMapping("/updateCateGory")
    public String edit(CateGory cateGory,Map map){
        cateGoryService.updateCateGory(cateGory);
        map.put("cateGory",cateGory);
        map.put("operatorId",cateGory.getCateGoryPid());
        return "forward:/category";
    }
}
